package com.example.zhinongbao.model;

import com.example.zhinongbao.R;
import com.example.zhinongbao.contract.HomeContract;

import java.util.ArrayList;
import java.util.List;

public class HomeModel implements HomeContract.Model {
    private static List<Product> products = new ArrayList<>(); // 改为静态变量保持数据

    public HomeModel() {
        // 初始化一些测试数据
        if (products.isEmpty()) {
            products.add(new Product(1, "有机苹果", 12.5, R.drawable.apple, "新鲜有机苹果", "山东烟台"));
            products.add(new Product(2, "绿色蔬菜", 8.0, R.drawable.vegetable, "无农药绿色蔬菜", "北京大兴"));
            products.add(new Product(3, "农家土鸡蛋", 15.0, R.drawable.egg, "散养土鸡蛋", "河北张家口"));
            products.add(new Product(4, "生态大米", 6.5, R.drawable.rice, "原生态种植大米", "黑龙江五常"));
        }
    }

    @Override
    public List<Product> getAllProducts() {
        return new ArrayList<>(products); // 返回副本避免外部修改
    }

    @Override
    public void addProduct(Product product) {
        products.add(product);
    }

    @Override
    public List<Product> searchProducts(String query) {
        List<Product> allProducts = getAllProducts();
        List<Product> results = new ArrayList<>();

        for (Product product : allProducts) {
            if (product.getName().toLowerCase().contains(query.toLowerCase())) {
                results.add(product);
            }
        }
        return results;
    }
}